package yiminsu.com.cn.utils;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.web.multipart.MultipartFile;

public class FileUtils {

	public static List<String> Fileupload(MultipartFile[] files){
		 List<String> listimage=new ArrayList<>();//中间表list
		for (int i = 0; i < files.length; i++) {
			if(!files[i].isEmpty()&&files[i].getOriginalFilename().length()>0){//图片不为空
				String contentType1=files[i].getContentType();
				String uuid1 =UUID.randomUUID().toString().replaceAll("-","");
				String suffixName1=contentType1.substring(contentType1.indexOf("/")+1);//后缀

				String  filename1= uuid1+"."+suffixName1;//截取图片名称及后缀
				String dir=GetDir();
				
				String path1=dir+"\\src\\main\\resources\\static\\homestay\\"+filename1;//图片下载地址
				
				String url="homestay/"+filename1;//外网访问路径
				
				File tempFile = new File(path1);//图片存放地址
				try {
					files[i].transferTo(tempFile);//保存图片
					listimage.add(url);//添加图片地址到list保存到民宿副表
				} catch (Exception e) {
					e.printStackTrace();
					
				}
			}
			
		}
		
		return listimage;
	}
	public static String Fileuploads(MultipartFile files){
		String str="";
		 if(!files.isEmpty()&&files.getOriginalFilename().length()>0){
     		String contentType3=files.getContentType();
				String uuid3 =UUID.randomUUID().toString().replaceAll("-","");
				String suffixName3=contentType3.substring(contentType3.indexOf("/")+1);//后缀
				
				String  filename3= uuid3+"."+suffixName3;//截取图片名称及后缀
				String dir=GetDir();
				
				String path1=dir+"\\src\\main\\resources\\static\\homestay\\"+filename3;//图片下载地址
				
				File tempFile = new File(path1);//图片存放地址
				try {
					files.transferTo(tempFile);//保存图片
					str="homestay/"+filename3;//拼接url
				} catch (Exception e) {
					e.printStackTrace();
					
				}
     	}
				
		return str;
	}
	
	
	//获取uuid
	public static String GetUUID(){
		
		return UUID.randomUUID().toString().replaceAll("-","");
		
	}
	//获取路径
	public static String GetDir(){
		
		return System.getProperty("user.dir");
		
	}
}
